package admin

import (
	"encoding/base64"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"os"
	"strconv"
	"time"
	"zhuandan/data_source"
	"zhuandan/models"
	"zhuandan/utils"
)

func Bannerlist(ctx *gin.Context) {
	var banner []models.Banner
	data_source.Db.Find(&banner)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    banner,
		"msg":     "ok",
	})
	return
}

//	func Tuiguangupdate(ctx *gin.Context) {
//		request := TuiguangupdateBody{}
//		ctx.ShouldBind(&request)
//		id := request.ID
//		bfb := request.Bfb
//		tuiguan := models.Tuiguang{
//			Bfb: bfb,
//		}
//		data_source.Db.Where("id = ?", id).Updates(&tuiguan)
//		ctx.JSON(http.StatusOK, gin.H{
//			"code":    0,
//			"error":   0,
//			"success": 200,
//			"data":    "ok",
//			"msg":     "ok",
//		})
//		return
//	}
type TuiguangupdateBody struct {
	ID  int64 `json:"id" from:"id"`
	Bfb int64 `json:"bfb" gorm:"bfb"`
}

func Provinces(ctx *gin.Context) {
	var city []models.City
	data_source.Db.Where("type = 1").Find(&city)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    city,
		"msg":     "ok",
	})
	return

}

type BanneraddBody struct {
	ID     int64  `gorm:"column:id" json:"id"`
	Status int64  `gorm:"column:status" json:"status"`
	Image  string `gorm:"column:image" json:"image"`
}

func Banneradd(ctx *gin.Context) {
	request := BanneraddBody{}
	ctx.ShouldBind(&request)
	image := request.Image
	status := request.Status
	ty := models.Banner{
		Image:  image,
		Status: status,
	}
	data_source.Db.Create(&ty)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Bannerupdate(ctx *gin.Context) {
	request := BanneraddBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	image := request.Image
	status := request.Status
	ty := models.Banner{
		Image:  image,
		Status: status,
	}
	data_source.Db.Where("id = ?", id).Updates(&ty)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}
func Bannerdel(ctx *gin.Context) {
	request := BanneraddBody{}
	ctx.ShouldBind(&request)
	id := request.ID
	data_source.Db.Where("id = ?", id).Delete(&models.Banner{})
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

func Base64ToStr(base64Str string) string {
	decoded, err := base64.StdEncoding.DecodeString(base64Str)
	if err != nil {
		panic(err)
	}
	return string(decoded)

}

type Person struct {
	Name string `json:"name"`
	Code string `json:"code"`
	Sub  []Sub  `json:"sub"`
}
type Sub struct {
	Name string `json:"name"`
	Code string `json:"code"`
	Sub1 []Sub1 `json:"sub"`
}
type Sub1 struct {
	Name string `json:"name"`
	Code string `json:"code"`
	Sub1 string `json:"sub"`
}

func Chuli(ctx *gin.Context) {
	file, err := os.Open("upload/citypicker1.json")
	if err != nil {
		fmt.Println("Open file error:", err)
		return
	}

	defer file.Close()
	var person []Person
	decoder := json.NewDecoder(file)
	err = decoder.Decode(&person)
	for _, v := range person {
		citynew1 := models.Citynew{
			Name:  v.Name,
			Code:  v.Code,
			Pid:   0,
			Level: 0,
		}
		data_source.Db.Create(&citynew1)

		for _, v1 := range v.Sub {
			citynew2 := models.Citynew{
				Name:  v1.Name,
				Code:  v1.Code,
				Pid:   citynew1.ID,
				Level: 1,
			}
			data_source.Db.Create(&citynew2)

			for _, v2 := range v1.Sub1 {
				citynew3 := models.Citynew{
					Name:  v2.Name,
					Code:  v2.Code,
					Pid:   citynew2.ID,
					Level: 2,
				}
				data_source.Db.Create(&citynew3)
			}

		}

	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return
}

type ShouxufeiRBody struct {
	Data       []models.Shouxufei `json:"data"`
	PageNo     int                `json:"pageNo"`
	PageSize   int                `json:"pageSize"`
	TotalCount int64              `json:"totalCount"`
	TotalPage  int64              `json:"totalPage"`
}

type ShouxufeiBody struct {
	Page        int     `json:"pageNo" from:"pageNo"`
	Pagesize    int     `json:"pageSize" from:"pageSize"`
	ID          int64   `json:"id" from:"id"`
	Sxf         float64 `gorm:"column:sxf" json:"sxf"`
	Adminid     int64   `gorm:"column:adminid" json:"adminid"`
	UserId      string  `gorm:"column:userId" json:"userId"`
	Adminuserid int64   `gorm:"column:adminuserid" json:"adminuserid"`
}

func Shouxufei(ctx *gin.Context) {
	request := ShouxufeiBody{}
	ctx.ShouldBind(&request)
	currentPage := request.Page
	pageSize := request.Pagesize
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Shouxufei
	res := data_source.Db
	res.Model(&models.Shouxufei{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := ShouxufeiRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}
func Shouxufeiup(ctx *gin.Context) {
	request := ShouxufeiBody{}
	ctx.ShouldBind(&request)
	sxf := request.Sxf
	id := request.ID
	data_source.Db.Model(&models.Shouxufei{}).Where("id = ?", id).Update("sxf", sxf)

	adminid := request.Adminid
	if adminid > 0 {
		adminlog := models.Adminlog{
			Adminid: adminid,
			Addtime: time.Now().Unix(),
			Content: "修改手续为：" + utils.FloatToStr64(sxf),
		}
		data_source.Db.Create(&adminlog)
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    "ok",
		"msg":     "ok",
	})
	return

}

type AdminlogsRBody struct {
	Data       []models.Adminlog `json:"data"`
	PageNo     int               `json:"pageNo"`
	PageSize   int               `json:"pageSize"`
	TotalCount int64             `json:"totalCount"`
	TotalPage  int64             `json:"totalPage"`
}

func Adminlogs(ctx *gin.Context) {
	request := ShouxufeiBody{}
	ctx.ShouldBind(&request)
	currentPage := request.Page
	pageSize := request.Pagesize
	adminid := request.Adminuserid
	userId := request.UserId
	offsetNum := pageSize * (currentPage - 1)
	var count int64
	var users []models.Adminlog
	res := data_source.Db
	if adminid > 0 {
		res = res.Where("adminid = ?", adminid)
	}
	if userId != "" {
		res = res.Where("content like ?", "%"+userId+"%")
	}
	res.Model(&models.Adminlog{}).Count(&count)
	res.Limit(pageSize).Offset(offsetNum).Order("id desc").Find(&users)

	var totalPage int64
	mo := count % int64(pageSize)
	if mo > 0 {
		totalPage = count/int64(pageSize) + 1
	} else {
		totalPage = count / int64(pageSize)
	}
	data := AdminlogsRBody{
		Data:       users,
		PageNo:     currentPage,
		PageSize:   pageSize,
		TotalCount: count,
		TotalPage:  totalPage,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

type IndexBody struct {
	Inttime    int64 `json:"inttime" from:"inttime"`
	Endtime    int64 `json:"endtime" from:"endtime"`
	Classifyid int64 `json:"classifyid" from:"classifyid"`
}

type IndexRBody struct {
	Zdnum          int64   `json:"zdnum"`
	Fdnum          int64   `json:"fdnum"`
	Xsnum          int64   `json:"xsnum"`
	Tdznum         int64   `json:"tdznum"`
	Ytdnum         int64   `json:"ytdnum"`
	Tspercentage   float64 `json:"tspercentage"`
	Xspercentage   float64 `json:"xspercentage"`
	Tdpercentage   float64 `json:"tdpercentage"`
	Fadane         float64 `json:"fadane"`         //发单额
	Xiaoshoue      float64 `json:"xiaoshoue"`      //销售额
	Tuidane        float64 `json:"tuidane"`        //退单额
	Pricexiahsoulv float64 `json:"pricexiahsoulv"` //金额销售率
	Pricetuidanlv  float64 `json:"pricetuidanlv"`  //金额退单率
	Gmrc           int64   `json:"gmrc"`           //购买人次
	Gmrs           int64   `json:"gmrs"`           //购买人数
	Llrs           int64   `json:"llrs"`           //浏览人数
	Scgmrs         int64   `json:"scgmrs"`         //首次购买人数
	Dcgmrs         int64   `json:"dcgmrs"`         //多次购买人数
	Gmzhl          float64 `json:"gmzhl"`          //购买转化率
	Fgl            float64 `json:"fgl"`            //复购率
	Usernum        int64   `json:"usernum"`        //总用户数
	Priceusernum   int64   `json:"priceusernum"`   //余额用户数
	Newusernum     int64   `json:"newusernum"`     //新增用户数
	Rhusernum      int64   `json:"rhusernum"`      //日活用户数
	Zhusernum      int64   `json:"zhusernum"`      //周活用户数
}

func Index(ctx *gin.Context) {
	request := IndexBody{}
	ctx.ShouldBind(&request)
	inttime := request.Inttime
	endtime := request.Endtime
	classifyid := request.Classifyid

	var procure []models.Procurestatus
	res := data_source.Db.Model(&models.Procure{}).Where("addtime >= ?", inttime).Where("addtime < ?", endtime)
	if classifyid > 0 {
		res = res.Where("classifyid = ?", classifyid)
	}
	res.Find(&procure)

	var user []models.Usersql
	data_source.Db.Model(&models.User{}).Where("create_time >= ?", inttime).Where("create_time < ?", endtime).Find(&user)

	var zdnum int64
	var fdnum int64
	var fadane float64

	var idarr []int64

	for _, p := range procure {
		if p.Fasong == 2 {
			fdnum = fdnum + 1
		}
		zdnum = zdnum + 1
		idarr = append(idarr, p.ID)
		fadane = fadane + p.Price*float64(p.Num)
	}

	var orders []models.Procureorder
	data_source.Db.Where("procureid in (?)", idarr).Find(&orders)
	var tdznum int64
	var ytdnum int64
	var xsnum int64
	var gmrc int64
	var gmrs int64
	var llrs int64
	var newusernum int64
	var xiaoshoue float64
	var tuidane float64
	var gmrsarr []int64
	var scgmrs int64
	var dcgmrs int64
	data_source.Db.Model(&models.Procurelog{}).Where("addtime >= ?", inttime).Where("addtime < ?", endtime).Count(&llrs)

	var rhusernum int64
	var zhusernum int64
	data_source.Db.Where("addtime >= ?", endtime-86400*7).Where("addtime < ?", endtime).Count(&zhusernum)
	data_source.Db.Where("addtime >= ?", endtime-86400).Where("addtime < ?", endtime).Count(&rhusernum)

	var uidarr []int64
	for _, u := range user {
		uidarr = append(uidarr, u.ID)
	}

	var orderuser []models.Procureorder
	data_source.Db.Where("uid in (?)", uidarr).Find(&orderuser)

	for _, u := range user {
		var cishu int64
		for _, o := range orderuser {
			if u.ID == o.Uid {
				cishu = cishu + 1
			}
		}
		if cishu == 1 {
			scgmrs = scgmrs + 1
		}
		if cishu > 1 {
			dcgmrs = dcgmrs + 1
		}
	}

	for _, o := range orders {
		if o.Status == 3 {
			tdznum = tdznum + 1
		}
		if o.Status == 4 {
			ytdnum = ytdnum + 1
			tuidane = tuidane + o.Price*float64(o.Num)
		}
		xsnum = xsnum + o.Num
		xiaoshoue = xiaoshoue + o.Price*float64(o.Num)
		gmrc = gmrc + 1

		cunzai := 1
		for _, g := range gmrsarr {
			if g == o.Uid {
				cunzai = 2
			}

		}
		if cunzai == 1 {
			gmrsarr = append(gmrsarr, o.Uid)
		}
	}

	gmrs = int64(len(gmrsarr))
	newusernum = int64(len(user))
	var user1 int64
	data_source.Db.Model(&models.User{}).Count(&user1)
	var user2 int64
	data_source.Db.Model(&models.User{}).Where("price > 0").Count(&user2)

	var tspercentage float64
	tspercentage = float64(fdnum) / float64(zdnum) * 100
	tspercentage1, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", tspercentage), 64)
	fdnum1 := fdnum * 4
	xspercentage := float64(xsnum) / float64(fdnum1) * 100
	xspercentage1, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", xspercentage), 64)

	tdpercentage := float64(ytdnum) / float64(xsnum) * 100
	tdpercentage1, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", tdpercentage), 64)

	pricexiahsoulv := xiaoshoue / fadane * 100
	pricexiahsoulv1, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", pricexiahsoulv), 64)
	var pricetuidanlv float64
	pricetuidanlv = float64(tuidane) / float64(xiaoshoue) * 100
	pricetuidanlv1, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", pricetuidanlv), 64)
	var gmzhl float64
	gmzhl = float64(gmrc) / float64(llrs) * 100
	gmzhl1, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", gmzhl), 64)

	var fglval float64
	if gmrs > 0 {
		var fgl float64
		fgl = float64(dcgmrs) / float64(gmrs) * 100
		fgl1, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", fgl), 64)
		fglval = fgl1
	}

	data := IndexRBody{
		Zdnum:          zdnum,
		Fdnum:          fdnum,
		Xsnum:          xsnum,
		Tdznum:         tdznum,
		Ytdnum:         ytdnum,
		Tspercentage:   tspercentage1,
		Xspercentage:   xspercentage1,
		Tdpercentage:   tdpercentage1,
		Fadane:         fadane,
		Xiaoshoue:      xiaoshoue,
		Tuidane:        tuidane,
		Pricexiahsoulv: pricexiahsoulv1,
		Pricetuidanlv:  pricetuidanlv1,
		Gmrc:           gmrc,
		Gmrs:           gmrs,
		Llrs:           llrs,
		Scgmrs:         scgmrs,
		Dcgmrs:         dcgmrs,
		Gmzhl:          gmzhl1,
		Fgl:            fglval,
		Usernum:        user1,
		Priceusernum:   user2,
		Newusernum:     newusernum,
		Rhusernum:      rhusernum,
		Zhusernum:      zhusernum,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

func Indexzx(ctx *gin.Context) {
	request := IndexBody{}
	ctx.ShouldBind(&request)
	inttime := request.Inttime
	endtime := request.Endtime

	var order []models.Procureorderindex
	data_source.Db.Model(&models.Procureorder{}).Where("addtime >= ?", inttime).Where("addtime < ?", endtime).Find(&order)

	allDateArray := make([]string, 0)
	timeFormat := "2006-01-02"
	startTime := time.Unix(inttime, 0)
	endTime := time.Unix(endtime, 0)
	//After方法 a.After(b) a,b Time类型 如果a时间在b时间之后，则返回true
	for endTime.After(startTime) {
		allDateArray = append(allDateArray, startTime.Format(timeFormat))
		startTime = startTime.AddDate(0, 0, 1)
	}
	allDateArray = append(allDateArray, endTime.Format(timeFormat))
	layout := "2006-01-02 15:04:05"
	var indexs []IndexzxRBody
	for _, v := range allDateArray {
		dateStr1 := v + " 00:00:00"
		// 解析日期字符
		t1, _ := time.Parse(layout, dateStr1)
		// 转换为Unix时间戳
		timestamp1 := t1.Unix() - 28800
		timestamp2 := timestamp1 + 86399

		var price float64
		for _, o := range order {
			if o.Addtime >= timestamp1 && o.Addtime < timestamp2 {
				price = price + o.Price*float64(o.Num)
			}
		}
		index := IndexzxRBody{
			Day:   v,
			Price: price,
		}
		indexs = append(indexs, index)
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    indexs,
		"msg":     "ok",
	})
	return

}

type IndexzxRBody struct {
	Day   string  `json:"day"`
	Price float64 `json:"price"`
}

type IndexuserRBody struct {
	Usernum     int64 `json:"usernum"`     //用户总数
	Dynum       int64 `json:"dynum"`       //订阅用户数
	Newnum      int64 `json:"newnum"`      //新用户数
	Buynum      int64 `json:"buynum"`      //购买用户数
	Dkhnum      int64 `json:"dkhnum"`      //大客户数
	Liebannum   int64 `json:"liebannum"`   //裂变用户数
	Dailinum    int64 `json:"dailinum"`    //代理商数
	Zhuandannum int64 `json:"zhuandannum"` //转单用户数
	Wanum       int64 `json:"wanum"`       //帮我挖用户数
	Ygnum       int64 `json:"ygnum"`       //员工
}

func Indexuser(ctx *gin.Context) {
	request := IndexBody{}
	ctx.ShouldBind(&request)
	inttime := request.Inttime
	endtime := request.Endtime
	inttime1, endtime1 := getCurrentdayStartAndEnd()
	var usernum int64

	var dynum int64
	data_source.Db.Model(&models.User{}).Where("guanzhu = 2").Select("id").Count(&dynum)
	var newnum int64

	var buynum int64
	data_source.Db.Model(&models.User{}).Where("ifbuy = ?", 2).Select("id").Count(&buynum)
	var dkhnum int64
	data_source.Db.Model(&models.User{}).Where("bnendtime > ?", time.Now().Unix()).Select("id").Count(&dkhnum)
	var liebannum int64
	var zhuandannum int64
	if inttime == 0 {
		data_source.Db.Model(&models.User{}).Select("id").Count(&usernum)
		data_source.Db.Model(&models.User{}).Where("pid > ?", 0).Select("id").Count(&liebannum)
		data_source.Db.Model(&models.User{}).Where("create_time >= ?", inttime1).Where("create_time < ?", endtime1).Select("id").Count(&newnum)
		data_source.Db.Model(&models.User{}).Where("routine_openid != ?", "").Select("id").Count(&zhuandannum)
	} else {
		data_source.Db.Model(&models.User{}).Where("pid > ?", 0).Select("id").Where("create_time >= ?", inttime).Where("create_time < ?", endtime).Count(&liebannum)
		data_source.Db.Model(&models.User{}).Where("create_time >= ?", inttime).Where("create_time < ?", endtime).Select("id").Count(&newnum)
		data_source.Db.Model(&models.User{}).Where("create_time >= ?", inttime).Where("create_time < ?", endtime).Select("id").Count(&usernum)
		data_source.Db.Model(&models.User{}).Where("create_time >= ?", inttime).Where("create_time < ?", endtime).Where("routine_openid != ?", "").Select("id").Count(&zhuandannum)
	}
	var wanum int64
	data_source.Db.Model(&models.User{}).Where("bangwowa = ?", 2).Select("id").Count(&wanum)
	data := IndexuserRBody{
		Usernum:     usernum,
		Dynum:       dynum,
		Newnum:      newnum,
		Buynum:      buynum,
		Dkhnum:      dkhnum,
		Liebannum:   liebannum,
		Dailinum:    0,
		Zhuandannum: zhuandannum,
		Wanum:       wanum,
		Ygnum:       0,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

// 今天开始结束时间戳
func getCurrentdayStartAndEnd() (startTime, endTime int64) {
	// 获取当前时间
	//今天开始
	startOfToday := time.Now().Truncate(24 * time.Hour)
	startTimestamp := startOfToday.Unix() - 28800
	// 获取今天的结束时间
	endOfToday := startOfToday.Add(24 * time.Hour).Add(-1 * time.Second)
	endTimestamp := endOfToday.Unix() - 28800

	return startTimestamp, endTimestamp
}

type IndexprovinceRBody struct {
	Fadannum    int64   `json:"fadannum"`    //发单量
	Kxsnum      int64   `json:"kxsnum"`      //可销售量
	Xsdnum      int64   `json:"xsdnum"`      //销售单量
	Fdl         float64 `json:"fdl"`         //发单率
	Xsl         float64 `json:"xsl"`         //销售率
	Fadanprice  float64 `json:"fadanprice"`  //发单金额
	Xsprice     float64 `json:"xsprice"`     //销售金额
	Fadanpricex float64 `json:"fadanpricex"` //39以下发单金额
	Xspricex    float64 `json:"xspricex"`    //39以下销售金额
	Xslx        float64 `json:"xslx"`        //39以下销售率
	Fadanprices float64 `json:"fadanprices"` //39以上发单金额(元)
	Xsprices    float64 `json:"xsprices"`    //39以上销售金额(元)
	Xsls        float64 `json:"xsls"`        //39以上销售率
}

func Indexprovince(ctx *gin.Context) {
	request := IndexBody{}
	ctx.ShouldBind(&request)
	inttime := request.Inttime
	endtime := request.Endtime
	var fadannum int64
	var kxsnum int64
	var xsdnum int64
	var fadanprice float64
	var fadanpricex float64
	var fadanprices float64
	var xsprice float64
	var xspricex float64
	var xsprices float64
	var xsl float64
	var xslx float64
	var xsls float64
	var fdl float64

	var procures []models.Procure
	data_source.Db.Where("addtime >= ?", inttime).Where("addtime < ?", endtime).Find(&procures)

	var procureidarr []int64
	for _, p := range procures {
		fadannum = fadannum + 1
		if p.Fasong > 1 {
			kxsnum = kxsnum + 1
			procureidarr = append(procureidarr, p.ID)
			fadanprice = fadanprice + p.Price*4
			if p.Price < 39 {
				fadanpricex = fadanpricex + p.Price*4
			} else {
				fadanprices = fadanprices + p.Price*4
			}
		}
	}

	var procureorders []models.Procureorder
	data_source.Db.Where("procureid in (?)", procureidarr).Where("status != 4").Find(&procureorders)
	for _, v := range procureorders {
		xsdnum = xsdnum + v.Num
		xsprice = xsprice + v.Price*float64(v.Num)
		if v.Price < 39 {
			xspricex = xspricex + v.Price*float64(v.Num)
		} else {
			xsprices = xsprices + v.Price*float64(v.Num)
		}
	}
	fdl = float64(kxsnum) / float64(fadannum)
	xsl = float64(xsdnum) / float64(xsdnum)
	xslx = xspricex / fadanpricex
	xsls = xsprices / fadanprices
	data := IndexprovinceRBody{
		Fadannum:    fadannum,
		Kxsnum:      kxsnum,
		Xsdnum:      xsdnum,
		Fdl:         fdl,
		Fadanprice:  fadanprice,
		Fadanpricex: fadanpricex,
		Fadanprices: fadanprices,
		Xsprice:     xsprice,
		Xspricex:    xspricex,
		Xsprices:    xsprices,
		Xsl:         xsl,
		Xsls:        xsls,
		Xslx:        xslx,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}
func getMonthStartAndEndTimestamps() (int64, int64) {
	// 获取当前时间
	now := time.Now()

	// 获取本月的第一天
	monthStart := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())

	// 获取下个月的第一天，然后减去一纳秒得到本月的最后一天的最后一刻
	nextMonthStart := time.Date(now.Year(), now.Month()+1, 1, 0, 0, 0, 0, now.Location())
	monthEnd := nextMonthStart.Add(-time.Nanosecond)

	// 转换为时间戳（以秒为单位）
	startTimestamp := monthStart.Unix()
	endTimestamp := monthEnd.Unix()

	return startTimestamp, endTimestamp
}
func getWeekStartAndEndTimestamps() (int64, int64) {
	// 获取当前时间
	now := time.Now()

	// 计算本周一的时间
	offset := int(time.Monday - now.Weekday())
	if offset > 0 {
		offset = -6 // 前一周的周一
	}
	weekStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).AddDate(0, 0, offset)

	// 计算本周日的时间
	weekEnd := weekStart.AddDate(0, 0, 6).Add(23*time.Hour + 59*time.Minute + 59*time.Second)

	// 转换为时间戳（以秒为单位）
	startTimestamp := weekStart.Unix()
	endTimestamp := weekEnd.Unix()

	return startTimestamp, endTimestamp
}

type IndexxiaoshouRBody struct {
	Usernum        int64 `json:"usernum"`        //总订阅用户数
	Newaddnum      int64 `json:"newaddnum"`      //新增订阅用户数
	Daynum         int64 `json:"daynum"`         //日活跃用户数
	Weeknum        int64 `json:"weeknum"`        //周活跃用户数
	Monthnum       int64 `json:"monthnum"`       //月活跃用户数
	Paynum         int64 `json:"paynum"`         //总购买用户数
	Daypaynum      int64 `json:"daypaynum"`      //今日购买用户数
	Newpaynum      int64 `json:"newpaynum"`      //首次购买用户数
	Priceusernum   int64 `json:"priceusernum"`   //余额用户数
	Dayrechargenum int64 `json:"dayrechargenum"` //今日充值用户数
}

func Indexxiaoshou(ctx *gin.Context) {

	request := IndexBody{}
	ctx.ShouldBind(&request)
	var users []models.User
	data_source.Db.Select("id,create_time,login_time,price,ifbuy").Find(&users)
	var usernum int64
	var newaddnum int64
	var daynum int64
	var weeknum int64
	var monthnum int64
	var paynum int64
	var daypaynum int64
	var newpaynum int64
	var priceusernum int64
	var dayrechargenum int64
	dayint, _ := getCurrentdayStartAndEnd()
	weekint, _ := getWeekStartAndEndTimestamps()
	monthint, _ := getMonthStartAndEndTimestamps()
	for _, u := range users {
		usernum = usernum + 1
		if u.CreateTime >= dayint {
			newaddnum = newaddnum + 1
		}
		if u.LoginTime >= dayint {
			daynum = daynum + 1
		}
		if u.LoginTime >= weekint {
			weeknum = weeknum + 1
		}
		if u.LoginTime >= monthint {
			monthnum = monthnum + 1
		}
		if u.Ifbuy > 1 {
			paynum = paynum + 1
		}
		if u.Price > 0 {
			priceusernum = priceusernum + 1
		}
	}

	data_source.Db.Model(&models.Procureorder{}).Where("addtime >= ?", dayint).Count(&daypaynum)
	data_source.Db.Model(&models.Buyorder{}).Where("addtime >= ?", dayint).Where("status = 2").Count(&dayrechargenum)
	data := IndexxiaoshouRBody{
		Usernum:        usernum,
		Newaddnum:      newaddnum,
		Daynum:         daynum,
		Weeknum:        weeknum,
		Monthnum:       monthnum,
		Paynum:         paynum,
		Daypaynum:      daypaynum,
		Newpaynum:      newpaynum,
		Priceusernum:   priceusernum,
		Dayrechargenum: dayrechargenum,
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}

type FenbuRBody struct {
	Citys     []models.Citynew  `json:"citys"`     //总订阅用户数
	Classifys []models.Classify `json:"classifys"` //总订阅用户数
}

func Fenbu(ctx *gin.Context) {

	request := IndexBody{}
	ctx.ShouldBind(&request)
	var citys []models.Citynew
	data_source.Db.Where("level = 0").Find(&citys)
	var tuisongs []models.Tuisongshezhi
	data_source.Db.Where("state = 1").Find(&tuisongs)
	var classifys []models.Classify
	data_source.Db.Find(&classifys)
	for k, c := range citys {
		var num int64
		for _, t := range tuisongs {
			if c.Code == utils.Int64string(t.Provinceid) {
				num = num + 1
			}
		}
		citys[k].Num = num
	}

	for k, cl := range classifys {
		var num int64
		for _, t := range tuisongs {
			if cl.ID == t.Classifyid {
				num = num + 1
			}
		}
		classifys[k].Num = num
	}
	data := FenbuRBody{
		Citys:     citys,
		Classifys: classifys,
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    data,
		"msg":     "ok",
	})
	return

}
